public class Sort {
    public static void insertSort(int[] array) {
        for(int i=1;i<array.length;i++){
            int tmp=array[i];
            int j=i-1;
            while(j>=0){
                if(array[j]>tmp){
                    array[j+1]=array[j];
                }else{
                    break;
                }
                j--;
            }
            array[j+1]=tmp;
        }
    }


    public static void shellSort(int[] array){
        int gap=array.length;
        while(gap>0){
            shell(array,gap);
            gap/=2;
        }
    }
    public static void shell(int[] array,int gap){
        for(int i=gap;i<array.length;i++){
            int tmp=array[i];
            int j=i-gap;
            while(j>=0){
                if(array[j]>tmp){
                    array[j+gap]=array[j];
                }else{
                    break;
                }
                j-=gap;
            }
            array[j+gap]=tmp;
        }
    }


    public static void selectSort(int[] array){
        for(int i=0;i<array.length;i++){
            int minIndex=i;
            int j=i+1;
            while(j<array.length){
                if(array[j]<array[minIndex]){
                    minIndex=j;
                }
                j++;
            }
            swap(array,i,minIndex);
        }
    }
    public static void swap(int[] arr,int i,int j){
        int tmp=arr[i];
        arr[i]=arr[j];
        arr[j]=tmp;
    }


    public static void heapSort(int[] array){
        createBigHeap(array);
        int end=array.length-1;
        while(end>0){
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }
    public static void createBigHeap(int[] array){
        for(int parent=(array.length-1-1)/2;parent>=0;parent--){
            shiftDown(array,parent,array.length);
        }
    }
    public static void shiftDown(int[] array,int parent,int len){
        int child=parent*2+1;
        while(child<len){
            if(child+1<len&&array[child]<array[child+1]){
                child++;
            }
            if(array[child]>array[parent]){
                swap(array,child,parent);
                parent=child;
                child=parent*2+1;
            }else{
                break;
            }
        }
    }


    public static void bubbleSort(int[] array){
        for(int i=0;i<array.length-1;i++){
            boolean flag=true;
            for(int j=0;j<array.length-i-1;j++){
                if(array[j]>array[j+1]){
                    swap(array,j,j+1);
                    flag=false;
                }
            }
            if(flag){
                return;
            }
        }
    }

    public static void quickSort(int[] array){
        quick(array,0,array.length-1);
    }
    public static void quick(int[] array,int start,int end){
        if(start>=end){
            return;
        }
        int pivot=partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }
    private static int partition(int[] array,int left,int right) {
        int tmp = array[left];
        while (left < right) {
            while (left< right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left< right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }


    public static void mergeSort(int[] array){
        mergeSortFunc(array,0,array.length-1);
    }
    public static void mergeSortFunc(int[] array,int left,int right){
        if(left>=right){
            return;
        }
        int mid=(left+right)/2;
        mergeSortFunc(array,left,mid);
        mergeSortFunc(array,mid+1,right);
        merge(array,left,right,mid);
    }
    public static void merge(int[] array,int start,int end,int mid){
        int s1=start;
        int s2=mid+1;
        int[] tmp=new int[end-start+1];
        int k=0;
        while(s1<=mid&&s2<=end){
            if(array[s1]<=array[s2]){
                tmp[k++]=array[s1++];
            }else{
                tmp[k++]=array[s2++];
            }
        }
        while(s1<=mid){
            tmp[k++]=array[s1++];
        }
        while(s2<=end){
            tmp[k++]=array[s2++];
        }
        for(int i=0;i<tmp.length;i++){
            array[i+start]=tmp[i];
        }
    }


    public static void countSort(int[] array){
        int max=array[0];
        int min=array[0];
        for(int i=1;i<array.length;i++){
            if(array[i]<min){
                min=array[i];
            }
            if(array[i]>max){
                max=array[i];
            }
        }
        int len=max-min+1;
        int[] count=new int[len];
        for(int i=0;i<array.length;i++){
            count[array[i]-min]++;
        }
        int index=0;
        for(int i=0;i<count.length;i++){
            while(count[i]>0){
                array[index]=i+min;
                index++;
                count[i]--;
            }
        }
    }


    public static void main(String[] args) {
        int[] arr={3,5,6,1,2,7,4,9,8,0};
        //insertSort(arr);
        //shellSort(arr);
        //selectSort(arr);
        //heapSort(arr);
        //bubbleSort(arr);
        //quickSort(arr);
        //mergeSort(arr);
        countSort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }
}
